package com.pzhu.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzhu.dormitory.dao.AbsentFromWorkDao;
import com.pzhu.dormitory.entity.AbsentFromWorkEntity;
import com.pzhu.dormitory.entity.StudentEntity;
import com.pzhu.dormitory.service.AbsentFromWorkService;
import com.pzhu.dormitory.service.StudentService;
import com.pzhu.dormitory.service.UserService;
import com.pzhu.dormitory.utils.PageUtils;
import com.pzhu.dormitory.utils.Query;
import com.pzhu.dormitory.vo.AbsentFromWorkVo;
import com.pzhu.dormitory.vo.UserVo;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("absentFromWorkService")
@AllArgsConstructor
public class AbsentFromWorkServiceImpl extends ServiceImpl<AbsentFromWorkDao, AbsentFromWorkEntity> implements AbsentFromWorkService {

    private final AbsentFromWorkDao absentFromWorkDao;
    private final UserService userService;
    private final StudentService studentService;

    @Override
    public PageUtils<AbsentFromWorkEntity> queryPage(Map<String, Object> params) {
        String stu = (String) params.get("stu");
        QueryWrapper<AbsentFromWorkEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isBlank(stu)) {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            UserVo userVo = userService.getUserInfoByUsername(authentication.getName());
            StudentEntity one = studentService.lambdaQuery().eq(StudentEntity::getSno, userVo.getUsername()).one();
            queryWrapper.lambda().eq(AbsentFromWorkEntity::getStuId, one.getId());
        }
        IPage<AbsentFromWorkEntity> page = this.page(
                new Query<AbsentFromWorkEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils<>(page);
    }

    @Override
    public PageUtils<AbsentFromWorkVo> queryPageAbsentFromWorkVo(Map<String, Object> params) {
        Page<AbsentFromWorkVo> page = new Query<AbsentFromWorkVo>().getPage(params);
        // 查询基本数据
        Page<AbsentFromWorkVo> roomVoPage = absentFromWorkDao.queryPageVo(page, params);
        return new PageUtils<>(roomVoPage);
    }

}
